Skip to content

Add Cloudflare D1 as a database source - #227

Merged
Skyfay merged 7 commits into
devfrom
Skyfay/cloudflare-d1
Nov 24, 2026
Merged

Skyfay merged 7 commits into
devfrom
Skyfay/cloudflare-d1

Conversation

@Skyfay

@Skyfay Skyfay commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Problem

DBackup could not back up Cloudflare D1. D1 runs only at Cloudflare, so there is no server to reach with a dump tool, and the only backup Cloudflare offers lives inside the same account.

Solution

A new database source, Cloudflare D1, that works through the REST API of Cloudflare alone, with an API token from a credential profile of the type Token. The connection is a Cloudflare account, and a job picks one, several or all of its D1 databases by name. Each database is exported by Cloudflare as a SQL dump, which DBackup stores unchanged as one entry of the archive. A restore replaces an existing database in place, so its ID and the Worker bindings stay, or creates a new one under a new name.

Details:

  • Backup polls the export of Cloudflare and streams the dump from R2. A token with D1 Read only gets a message that names the missing permission D1 Edit, and a database with an FTS5 table gets an explanation, since Cloudflare cannot export virtual tables.
  • The export of Cloudflare rounds integers beyond 2^53. A check before each export names every column that holds one in the run log, and the connection can turn it off.
  • A restore over an existing database streams the drops of its tables, views and triggers ahead of the dump into one import, which Cloudflare runs as one transaction. A failed import leaves the database as it was. The Time Travel bookmark from before goes into the log, with the command back to it.
  • A restore under a new name creates the database in the jurisdiction of the original, or where the connection says (EU, FedRAMP or a location hint).
  • The Database Explorer reads D1 through the query API, read only, with large integers as text and BLOBs cut to 1 KB.
  • Transfer URLs from the API are only used on R2 over HTTPS, the token and the signed URLs never reach a log, and a stored token does not follow a change of the account ID.
  • Dev lab: with cloudflareD1 in the lab config the seed fills dbackup-lab-shop in the test account and adds the source and a job to Local.
  • End-to-end: a new family cloud against a test account at Cloudflare. Only its CI job gets the token (E2E_D1_TOKEN, E2E_D1_ACCOUNT_ID), and without one the test skips.
  • Docs: source guide, index pages, developer guide, README, website card in English and German, changelog fragment.

Testing

  • pnpm validate and pnpm run build pass, with unit tests for the API client, backup, restore, the Explorer and the address of stored secrets against a stand-in for Cloudflare.
  • Against a Cloudflare test account: backup, restore under a new name with the EU jurisdiction, restore over a changed database, restore of a broken dump that left the target unchanged, Explorer, wrong token.
  • pnpm lab:seed, pnpm lab:check and the lab job through the real runner.
  • E2E_FAMILIES=cloud pnpm test:e2e passed on the lab before and after the changes from the security review.
  • Tried in the UI by hand: connection, backup, restore in place and under a new name.

Backs up and restores the D1 databases of a Cloudflare account through the REST API of Cloudflare, with an API token from a Token credential profile. The export is polled and its SQL dump stored unchanged. A restore into an existing database puts the drops ahead of the dump in one import, so a failed import leaves the database as it was, and logs the Time Travel bookmark from before. A new name creates the database in the jurisdiction of the source or where the connection says. A check before the export warns about integers beyond 2^53, which the export of Cloudflare rounds.
With cloudflareD1 in the lab config the seed fills the database dbackup-lab-shop of the test account with the SQLite dataset and adds the source Lab Cloudflare D1 and a job to Local, which lab:check tests and runs like the other lab connections.
The family cloud backs up a database of a Cloudflare test account, restores it under a new name and over a changed copy, and compares both through the API of Cloudflare. It reads the account from E2E_D1_ACCOUNT_ID and E2E_D1_TOKEN or the lab config and skips without one, and only its CI job gets the token.
A restore streams the drops ahead of the dump into the upload instead of writing a second file, so no decrypted copy lands in the temp folder. A stored token no longer follows a change of the account ID. The Database Explorer reads at most 16 MB per page and shows BLOBs cut to 1 KB, polls are tried again with a pause after a failure on the way, and a database a restore created stays when Cloudflare may still finish the import. URLs in the messages of Cloudflare are left out of the run log.
Compose up without a service starts every service of the project, so the family cloud started the whole lab on the runner beside the build of the image.
The export of Cloudflare writes a NUL character of a text raw into the dump, and the import stops reading at it and still reports success, so a restore writes such a character as char(0). Every import now ends with a check table, and a restore that did not run to the end fails instead of reporting success. The check before a backup also names values too long for the import of D1, the code 7500 no longer reads as a missing permission, and the guide gives the measured lock times.
Tests for an API that times out, breaks off or answers too much, the bound of the database list, ping and the job form, the value check, failed exports, downloads and uploads, where a restore puts a new database, the order of the drops and the restore of earlier versions. The unused batch and getDatabase helpers are gone.
@Skyfay
Skyfay merged commit 8325d10 into dev Nov 24, 2026
22 checks passed

This branch was successfully deployed

1 active deployment
e2e — 0b409776 Deployed Nov 24, 2026 by Skyfay via Approve #170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant